<template>
<!-- 个人档案-修改账号 -->
  <el-dialog
    custom-class="set-password-comp"
    title="修改密码"
    :close-on-click-modal="false"
    append-to-body
    :visible="visible"
    :width="$setRem(450)"
    :before-close="handleCancel"
  >
    <div class="form-container">
      <el-form
        :model="ruleForm"
        :rules="rules"
        size="medium"
        ref="ruleForm"
        label-width="120px"
        class="rule-form"
      >
        <el-form-item label="原密码" prop="oldPassword" required>
          <el-input
            type="password"
            placeholder="输入原密码"
            v-model="ruleForm.oldPassword"
            :show-password="true"
            clearable
            autocomplete="off"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" required>
          <el-input
            type="password"
            placeholder="请输入新密码"
            v-model="ruleForm.newPassword"
            :show-password="true"
            clearable
            autocomplete="off"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password" required>
          <el-input
            type="password"
            placeholder="请输入新密码"
            v-model="ruleForm.password"
            :show-password="true"
            clearable
            autocomplete="off"
          >
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleCancel">取 消</el-button>
      <el-button
        type="primary"
        size="medium"
        :loading="loading"
        @click="hanldeSave"
      >
        确 认
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "setPassword",
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: true
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // form表单数据
      ruleForm: {
        oldPassword: "",
        newPassword: "",
        password: ""
      },
      // form 表单校验规则
      rules: {
        oldPassword: [{ required: true, message: "请输入原密码" }],
        newPassword: [
          { required: true, message: "请输入新密码" },
          { validator: this.validateNewPass, trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入新密码" },
          { validator: this.validatePass, trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.userInfo;
    }
  },
  watch: {
    // 监听弹框显示/隐藏
    visible(data) {
      if (!data) {
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = "";
        });
      }
    }
  },
  methods: {
    validateNewPass(rule, value, callback) {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.password !== "") {
          this.$refs.ruleForm.validateField("password");
        }
        callback();
      }
    },
    validatePass(rule, value, callback) {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    },
    // 保存
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const { newPassword, oldPassword } = this.ruleForm;
          this.$emit("submit", {
            newPassword,
            oldPassword,
            id: this.userInfo.uuid
          });
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit("cancel");
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../assets/styles/dialog";
.set-password-comp {
  .el-input {
    width: calc(100% - 40px);
  }
}
</style>
